package com.sq.partner.manager.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.RuleAgent;
import com.sq.partner.manager.service.RuleAgentConfigService;
import com.sq.partner.manager.util.DateUtil;
import com.sq.partner.manager.util.EncryptUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.QRcodeUtil;
import com.sq.partner.manager.util.SqlUtil;

import freemarker.template.Configuration;

@Controller
@RequestMapping(value="/ruleAgentConfig")
public class RuleAgentController {
	private static Logger logger=LogUtil.getLog(RuleAgentController.class);
	@Resource
	private RuleAgentConfigService ruleAgentConfigService;
	/**
	 * 跳转方案列表页面
	 */
	@RequestMapping(value = "/list")
	public String list() throws Exception {
		return "facadeManage/ruleAgent/list";
	}
	
	@RequestMapping(value="/queryRulePage")
	@ResponseBody
	public Page<Map<String,Object>> queryRulePageByCriteria(HttpSession session,Page<Map<String,Object>> page,String planNo,String ruleName) throws Exception{
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		String deptCode = user.getDepartmentStr();
		String userCodes=com.sq.partner.manager.common.Config.getProp("imple.nlife.userCodes");
		boolean flag=false;
		logger.info("登录用户名："+user.getUserCode()+"   配置的用户名： "+userCodes);
		Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
		paramMap.put("deptCode", deptCode==null?"''":deptCode.substring(1, 3));
		paramMap.put("ruleName", ruleName==null?"":ruleName);
		paramMap.put("planCode", planNo==null?"":planNo);
		page.setMap(paramMap);
		page.setObj(flag);
		page = ruleAgentConfigService.queryRulePageByCriteria(page);
		return page;
	} 
	@RequestMapping(value="/queryRuleAgentPage")
	@ResponseBody
	public Page<Map<String,Object>> queryRuleAgentPageByCriteria(HttpSession session,Page<Map<String,Object>> page,RuleAgent ruleAgent) throws Exception{
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		String deptCode = user.getDepartmentStr();
		String userCodes=com.sq.partner.manager.common.Config.getProp("imple.nlife.userCodes");
		boolean flag=false;
		logger.info("登录用户名："+user.getUserCode()+"   配置的用户名： "+userCodes);
		Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
		paramMap.put("deptCode", deptCode==null?"''":deptCode.substring(1, 3));
		paramMap.put("partnerNo", ruleAgent.getPartnerNo()==null?"":ruleAgent.getPartnerNo());
		paramMap.put("ruleName", ruleAgent.getRuleName()==null?"":ruleAgent.getRuleName());
		paramMap.put("ruleId", ruleAgent.getRuleId()==null?"":ruleAgent.getRuleId());
		paramMap.put("userCode", ruleAgent.getUserCode()==null?"":ruleAgent.getUserCode());
		paramMap.put("checkStatus", ruleAgent.getCheckStatus()==null?"":ruleAgent.getCheckStatus());
		paramMap.put("premium", ruleAgent.getPremium()==null?"":ruleAgent.getPremium());
		page.setMap(paramMap);
		page.setObj(flag);
		page = ruleAgentConfigService.queryRuleAgentPageByCriteria(page);
		return page;
	} 
	@RequestMapping("/delRuleAgent")
	@ResponseBody
	public Result delRuleAgentById(String ruleAgentId) throws Exception{
		return ruleAgentConfigService.delRuleAgentById(ruleAgentId);
	}
	
	@RequestMapping(value = "/toAddRuleAgent")
	public String toAddRuleAgent() throws Exception {
		return "facadeManage/ruleAgent/add";
	}
	@RequestMapping(value = "/addRuleAgent")
	@ResponseBody
	public Result saveRuleAgent(HttpSession session,String ruleId,String[] ruleAgentIds,String effDate,String termDate,String premium,String ruleAgentDesc,String status) throws Exception {
		RuleAgent ruleAgent = new RuleAgent();
		ruleAgent.setRuleId(ruleId);
		ruleAgent.setPremium(premium);
		ruleAgent.setStatus(status);
		ruleAgent.setRuleAgentIds(ruleAgentIds);
		if(effDate!=null&&!"".equals(effDate)){
			ruleAgent.setEffDate(DateUtil.parse(effDate));
		}
		if(termDate!=null&&!"".equals(termDate)){
			ruleAgent.setTermDate(DateUtil.parse(termDate));
		}
		if(ruleAgentDesc!=null&&!"".equals(ruleAgentDesc)){
			ruleAgent.setRuleAgentDesc(ruleAgentDesc);
		}
		return 	ruleAgentConfigService.saveRuleAgentById(ruleAgent);
	}
	@RequestMapping(value = "/updateRuleAgent")
	@ResponseBody
	public Result updateRuleAgent(HttpSession session,String ruleAgentId,String effDate,String termDate,String ruleAgentDesc,String status,String premium) throws Exception {
		RuleAgent ruleAgent = new RuleAgent();
		ruleAgent.setStatus(status);
		ruleAgent.setRuleAgentId(Long.parseLong(ruleAgentId));
		if(effDate!=null&&!"".equals(effDate)){
			ruleAgent.setEffDate(DateUtil.parse(effDate));
		}else{
			ruleAgent.setEffDate(null);
		}
		if(termDate!=null&&!"".equals(termDate)){
			ruleAgent.setTermDate(DateUtil.parse(termDate));
		}else{
			ruleAgent.setTermDate(null);
		}
		if(ruleAgentDesc!=null&&!"".equals(ruleAgentDesc)){
			ruleAgent.setRuleAgentDesc(ruleAgentDesc);
		}else{
			ruleAgent.setRuleAgentDesc("");
		}
		if(premium!=null&&!"".equals(premium)){
			ruleAgent.setPremium(premium);
		}else{
			ruleAgent.setPremium("");
		}
		return 	ruleAgentConfigService.updateRuleAgent(ruleAgent);
	}
	@RequestMapping(value = "/queryAgentInfoByPlanNo")
	@ResponseBody
	public Page<Map<String,Object>> queryAgentInfoByPlanNo(HttpSession session,Page<Map<String,Object>> page,String planNo,String ruleId) throws Exception{
			GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
			String deptCode = user.getDepartmentStr();
			String userCodes=com.sq.partner.manager.common.Config.getProp("imple.nlife.userCodes");
			boolean flag=false;
			logger.info("登录用户名："+user.getUserCode()+"   配置的用户名： "+userCodes);
			Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
			paramMap.put("deptCode", deptCode==null?"''":deptCode.substring(1, 3));
			paramMap.put("planNo", planNo==null?"":planNo);
			paramMap.put("ruleId", ruleId==null?"":ruleId);
			paramMap.put("agentType", "3");
			page.setMap(paramMap);
			page.setObj(flag);
			page = ruleAgentConfigService.queryAgentInfoByPlan(page);
			return page;
		} 
	@RequestMapping(value = "/toeditRuleAgent")
	public String toeditRuleAgent(Model model, String ruleAgentId) throws Exception {
		Map<String, Object> agentInfo = ruleAgentConfigService.queryAgentInfoById(ruleAgentId);
		model.addAllAttributes(agentInfo);
		
		return "facadeManage/ruleAgent/edit";
	}
	@RequestMapping(value = "/downloadQRCode")
	public void downloadQRCode( String ruleAgentId,HttpServletResponse response) throws Exception {
		Map<String, Object> agentMap = ruleAgentConfigService.queryAgentInfoById(ruleAgentId);
		String picName="";
		if(agentMap.get("USER_NAME")!=null){
			picName=(String) agentMap.get("USER_NAME");
		}else{
			picName=(String) agentMap.get("USERCODE");
		}
		String sign=EncryptUtil.encrypt((String) agentMap.get("SIGN"));
		sign=EncryptUtil.base64Encode(sign);
		String classPath=this.getClass().getResource("").getPath();
		String url = (classPath.substring(0,classPath.indexOf("/WEB-INF/")+8)+ "/images/logo.png").replace("/WEB-INF", "");
		QRcodeUtil.QRcodeToStream1(response, Config.getProp("XPXEWM.URL")+sign, "", picName, 300, 300,url);
	}
}
